package com.weasel.weixin.mp.servlet;

import com.weasel.weixin.commons.helper.SignatureChecker;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Dylan on 2015/9/28.
 */
public class SignatureCheckServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String signature = request.getParameter("signature");
        String echostr = request.getParameter("echostr");

        MpConfig config = MpConfigBuilder.fromXML("classpath:weixin-config.xml");
        boolean checkSuccess = SignatureChecker.check(config.getToken(),timestamp,nonce,signature);

        if(checkSuccess){
            response.getWriter().print(echostr);
        }

    }
}
